package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.text.TextUtils;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.transport.GoodsInfoTypeEntity3;
import com.sgb.kjwl.view.ui.adapter.transport.GoodsTypeAdapter3;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.List;

public class GoodsTypePopWindow3 extends BasePopWindow {

    private GoodsTypeAdapter3 goodsTypeAdapter;
    private GoodsInfoTypeEntity3 currentItem;

    public GoodsTypePopWindow3(Context context) {
        super(context);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_goods_types;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        RecyclerView recyclerView = holder.getPop().findViewById(R.id.type_list);

        goodsTypeAdapter = new GoodsTypeAdapter3(mContext, null);
        goodsTypeAdapter.setItemSelect(true);
        goodsTypeAdapter.setDefItem(0);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext, 4));
        recyclerView.setAdapter(goodsTypeAdapter);

        goodsTypeAdapter.setOnItemClick(new AdapterOnItemClick<GoodsInfoTypeEntity3>() {
            @Override
            public void onItemClick(GoodsInfoTypeEntity3 provinceEntity, int position) {
                onResultClick.result(provinceEntity, position, null);
                currentItem = provinceEntity;
                dissMiss();
            }
        });

        //设置背景
        setBgView(holder.getPop().findViewById(R.id.bg_pop_gray_layout));
    }

    public void setData(List<GoodsInfoTypeEntity3> list, int position) {
        goodsTypeAdapter.setDatas(list);
        goodsTypeAdapter.setDefItem(position);
    }

    public void setDefItem(int position) {
        goodsTypeAdapter.setDefItem(position);
    }

    public boolean isSetGoodstype() {
        return currentItem != null;
    }

    public String getGoodsTypeStr() {
        return currentItem.getName();
    }

    public void resetSelectItem() {
        goodsTypeAdapter.setDefItem(0);
        goodsTypeAdapter.resetCheckDatas();
    }

    /**
     * 为货源类型弹窗设置选中回显效果（带不限）
     *
     * @param selectTypeNo
     */
    public void setSelectTypeNo(String selectTypeNo) {
        if (goodsTypeAdapter != null) {
            goodsTypeAdapter.setItemSelect(true);
            List<GoodsInfoTypeEntity3> list = goodsTypeAdapter.getDatas();
            if (list == null)
                return;
            goodsTypeAdapter.resetCheckDatas();
            if (list.size() > 0) {
                if (TextUtils.isEmpty(selectTypeNo) || selectTypeNo.equals("Default_GoodsType")) {
                    //选择为空或者不限
                    goodsTypeAdapter.setDefItem(0);
                } else { //选择其他类型
                    for (int i = 0; i < list.size(); i++) {
                        GoodsInfoTypeEntity3 data = list.get(i);
                        if (!TextUtils.isEmpty(data.getNo()) && data.getNo().equals(selectTypeNo)) {
                            goodsTypeAdapter.setDefItem(i);
                            return;
                        }
                    }
                }
            }
        }
    }
}
